﻿<#
	File      = ConvertTo-WmiFilter.ps1
	Author    = Kirk "Poshoholic" Munro
	Publisher = Poshoholic Studios
	Copyright = © 2012 Poshoholic Studios. All rights reserved.
#>

function ConvertTo-WmiFilter {
	[CmdletBinding(DefaultParameterSetName='SingleProperty')]
	[OutputType([System.String])]
	param(
		[Parameter(Mandatory=$true,Position=0,ParameterSetName='SingleProperty')]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$PropertyName,

		[Parameter(ParameterSetName='SingleProperty')]
		[ValidateNotNullOrEmpty()]
		[System.String[]]
		$FilterValue,

		[Parameter(ParameterSetName='SingleProperty')]
		[ValidateNotNullOrEmpty()]
		[System.String[]]
		$LiteralFilterValue,

		[Parameter(Mandatory=$true,ParameterSetName='MultipleProperties')]
		[ValidateNotNullOrEmpty()]
		[System.Collections.Hashtable]
		$PropertyMap
	)
	try {
		switch ($PSCmdlet.ParameterSetName) {
			'SingleProperty' {
				$wmiFilterSet = @()
				if (-not $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('FilterValue') -and -not $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('LiteralFilterValue')) {
					[System.String]$message = $PSCmdlet.GetResourceString('ParameterBinderStrings','MissingMandatoryParameter')
					[System.Management.Automation.ParameterBindingException]$exception = Microsoft.PowerShell.Utility\New-Object -TypeName System.Management.Automation.ParameterBindingException -ArgumentList ($message -f $null,' FilterValue')
					[System.Management.Automation.ErrorRecord]$errorRecord = Microsoft.PowerShell.Utility\New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception,'ParameterBindingException',([System.Management.Automation.ErrorCategory]::InvalidArgument),$null
					$PSCmdlet.ThrowTerminatingError($errorRecord)
				}
				if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('FilterValue') -and $FilterValue) {
					foreach ($item in $FilterValue) {
						if ([System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters($item)) {
							$wmiEquivalentItem = $item -replace '``','¢' -replace '^\*','%' -replace '^\?','_' -replace '([^`]{1}?)\*','$1%' -replace '([^`]{1}?)\?','$1_' -replace '`\*','*' -replace '`\?','?' -replace '¢','`' -replace '\\','\\'
							$wmiFilterSet += "$PropertyName LIKE '$wmiEquivalentItem'"
						} else {
							$wmiEquivalentItem = [System.Management.Automation.WildcardPattern]::Unescape($item) -replace '\\','\\'
							$wmiFilterSet += "$PropertyName = '$wmiEquivalentItem'"
						}
					}
				}
				if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('LiteralFilterValue') -and $LiteralFilterValue) {
					foreach ($item in $LiteralFilterValue) {
						$wmiEquivalentItem = $item -replace '\\','\\'
						$wmiFilterSet += "$PropertyName = '$wmiEquivalentItem'"
					}
				}
				$wmiFilterSet -join ' OR '
				break
			}
			'MultipleProperties' {
				$wmiFilterSet = @()
				foreach ($key in $PropertyMap.Keys) {
					$wmiFilterSet += "($(ConvertTo-WmiFilter -PropertyName $key -FilterValue $PropertyMap.$key))"
				}
				$wmiFilterSet -join ' AND '
				break
			}
		}
	} catch {
		throw
	}
}